﻿using System;
using System.Collections;
using System.Data;
using visinia.SmartEngine.Cache;
using visinia.SmartEngine.Security;
using viForum.Providers;
using visinia.SmartEngine.Installer;

namespace viForum.Controllers
{
    public class BaseController
    {
        static bool bEventRegistered = false;
        public BaseController()
        {
            if (!bEventRegistered)
            {
                //MembershipController.UserAdded += new EventHandler<EventArgs>(UserController.AddUser);
                MembershipController.UserDeleted += new EventHandler<EventArgs>(UserController.DeleteUser);
                bEventRegistered = true;
            }
        }        

        public static string viForumUrlSeparator
        {
            get{ return PluginController.GetInstalledPlugin("viForum").UrlSeparator; }
        }

        /// <summary>
        /// The page name of the forum used internally
        /// </summary>
        public static string ForumPageName
        {
            get { return "vf_FrontPage"; }
        }

        /// <summary>
        /// The forum page id used internally
        /// </summary>
        public static int ForumPageId
        {
            get { return Convert.ToInt32(Settings["vf_ForumPageId"]); }
            set { AddSetting("vf_ForumPageId", value.ToString()); }
        }

        /// <summary>
        /// The page name of the forum thread list used internally
        /// </summary>
        public static string ForumThreadListPageName
        {
            get { return "vf_ThreadListPage"; }
        }

        /// <summary>
        /// The forum threads list page id used internally
        /// </summary>
        public static int ForumThreadListPageId
        {
            get { return Convert.ToInt32(Settings["vf_ForumThreadListPageId"]); }
            set { AddSetting("vf_ForumThreadListPageId", value.ToString()); }
        }

        /// <summary>
        /// The page name of the forum single thread used internally
        /// </summary>
        public static string ForumThreadPageName
        {
            get { return "vf_ThreadPage"; }
        }

        /// <summary>
        /// The forum thread page id used internally
        /// </summary>
        public static int ForumThreadPageId
        {
            get { return Convert.ToInt32(Settings["vf_ForumThreadPageId"]); }
            set { AddSetting("vf_ForumThreadPageId", value.ToString()); }
        }

        /// <summary>
        /// The page name of the forum editor used internally
        /// </summary>
        public static string ForumEditorPageName
        {
            get { return "vf_EditorPage"; }
        }

        /// <summary>
        /// The forum editor page id used internally
        /// </summary>
        public static int ForumEditorPageId
        {
            get { return Convert.ToInt32(Settings["vf_ForumEditorPageId"]); }
            set { AddSetting("vf_ForumEditorPageId", value.ToString()); }
        }

        /// <summary>
        /// The forum theme
        /// </summary>
        public static string ForumTheme
        {
            get { return (string)Settings["vf_Theme"]; }
            set { AddSetting("vf_Theme", value.ToString()); }
        }

        /// <summary>
        /// Gets the forum setting
        /// </summary>
        /// <param name="key">The settings key to get</param>
        /// <returns>returns the setting value</returns>
        public static string GetSetting(string key)
        {
            if (Settings.Contains(key))
                return (string)Settings[key];
            else
                return string.Empty;
        }

        public const string KEY_FORUM_SETTINGS = "vf_settings";
        private static Hashtable _Settings;
        private static Hashtable Settings
        {
            get
            {
                _Settings = (Hashtable)DataCache.GetCache(KEY_FORUM_SETTINGS);
                if (_Settings == null)
                {
                    _Settings = new Hashtable();
                    IDataReader dr = viForumDataProvider.Provider.GetForumSettings();
                    while (dr.Read())
                    {
                        if (dr["SettingName"] != Convert.DBNull)
                        {
                            if (dr["SettingValue"] != Convert.DBNull)
                                _Settings[(string)dr["SettingName"]] = (string)dr["SettingValue"];
                            else
                                _Settings[(string)dr["SettingName"]] = "";
                        }
                    }
                    dr.Close();
                    DataCache.SetCache(KEY_FORUM_SETTINGS, _Settings);
                }
                return _Settings;
            }
        }

        #region DATA
        /// <summary>
        /// Adds a new forum setting Or updates it if it already exists
        /// </summary>
        /// <param name="key">The key to setting</param>
        /// <param name="value">The value of the setting</param>
        public static void AddSetting(string key, string value)
        {
            if (Settings.ContainsKey(key))
                viForumDataProvider.Provider.UpdateForumSetting(key, value);
            else
                viForumDataProvider.Provider.AddForumSetting(key, value);
            DataCache.DeleteCacheMultiple(KEY_FORUM_SETTINGS);
        }
        /// <summary>
        /// Deletes the site setting
        /// </summary>
        /// <param name="key">The key to delete</param>
        public static void DeleteSetting(string key)
        {
            viForumDataProvider.Provider.DeleteForumSetting(key);
            DataCache.DeleteCacheMultiple(KEY_FORUM_SETTINGS);
        }
        #endregion
    }
}
